/**
 * Created by luodan on 2015/5/16 0016.
 */
define('list', function() {
    return list = avalon.define({
        $id: 'list',
        active: 1,
        modelActive:1,
        deleteFlag :0,
        // 使当前标签点亮
        addActive: function(id) {

            //先清空orderList数组
            list.active = id;
            list.modelActive = id;
        },

        MUrl: '',    // 模态框url

        ready:function() {
            list.orderList = [];  // 清空数组
            list.Page = 1;        // 页面数置为1
            list.getKeyValue();
            list.getOrderList(list.active);
            list.modelActive = list.active;
        },

        keyValue: "受理",
        getKeyValue: function() {
            switch (list.modelActive) {
                case 1 :
                    list.keyValue="受理";
                    break;
                case 2 :
                    list.keyValue="诊断";
                    break;
                case 3 :
                    list.keyValue="开始维修";
                    break;
                case 4 :
                    list.keyValue="测试";
                    break;
                case 5 :
                    list.keyValue="测试完成";
                    break;
                case 6 :
                    list.keyValue="交付完成";
                    break;
                case 7 :
                    list.keyValue="交付完成";
                    break;
                case 8 :
                    list.keyValue="重新受理";
                    break;
                default :
                    break;

            }
        },

        isCancel: false,    //  用于判断是否显示取消原因模态框
        // 模态框切换为-取消原因模态框
        showCancel: function() {
            list.isCancel = true;
        },

        // 模态框切换为-详情模态框
        showInfo: function() {
            list.isCancel = false;
        },

        //关闭模态框将deleteFlag 置为0
        setDeleteFlag: function(){
            list.deleteFlag = 0;
            list.isCancel = false;
        },


        //诊断列表信息
        diagnoseList:[],
        answer:"",
        username:"",

        //添加诊断结果和诊断人姓名
        addDiagnose: function(){

            //判断输入内容是否为空
            list.diagnoseList.push({
                ans: list.answer,
                un: list.username
            });
        },

        //添加处理过程
        add : function(){
            $.call({
                type: 'post',
                i: "7AddLog",
                data: {
                    Type:list.active,
                    Content:list.answer,
                    OrderID:""
                },
                success: function(data){
                    if (data.c == 200) {
                        tip.on('添加成功！',1,4000);
                        //将输入信息显示为静态文本
                        list.addDiagnose();
                    }
                    else {
                        tip.on('添加失败，请重新提交！',0,6000);
                    }
                }

            });
        },

        orderList:[],
        Page : 1,
        Total: 0,
        PageNumber : 10,

        //获取订单列表信息
        getOrderList : function(){
            $.call({
                type: 'post',
                i: "7GetOrderByStatus",
                data: {
                    Status : list.active,
                    Page : list.Page,
                    PageNumber : list.PageNumber
                },
                success: function(data){
                    if (data.c == 200) {
                        list.orderList.push.apply(list.orderList,data.d.List);
                        list.Total = data.d.Total;
                        list.showButton();
                    }
                    else {
                        tip.on('添加失败，请重新提交！',0,6000);
                    }
                }

            });
        },

        //是否显示加载更多按钮
        showMoreStatus:0,

        //判断是否显示按
        showButton : function(){
            list.showMoreStatus = (list.Total<=list.PageNumber || list.Page*list.PageNumber>=list.Total) ? 0: 1;
        },

        //钮加载更多
        showMore : function(){
            list.Page++;
            list.getOrderList();
        },

        /************************************获取订单详细信息******************************************/
        order:{},  // 订单详细信息
        Accessories: [], //配件信息
        repairLog:[], //维修记录
        index: 0,  // 数组下角标
        getOrder: function(orderID,index){
            list.index = index;
            $.call({
                type: 'post',
                i: "7GetOrder",
                data: {
                    OrderID:orderID
                },
                success: function(data){
                    if (data.c == 200) {
                       list.order = data.d;
                        list.Accessories = data.d.Accessories;
                        list.CTime = data.d.Time;
                        list.repairLog = data.d.Log;
                        list.getLogByType();

                        // 重新获取按钮名称
                        list.getKeyValue();
                        //alert(list.order.T2.date);
                    }
                    else {
                        tip.on('获取订单详细信息失败',0,6000);
                    }
                }
            });
        },

        //创建日期
        CTime:"",
        //受理日期
        GTime:"",
        //受理人
        GMan:"",
        //诊断开始日期
        DTime:"",
        //诊断人
        DMan:"",
        //诊断数组
        DArray:[],
        //维修开始日期
        FTime:"",
        //维修人
        FMan:"",
        //维修数组
        FArray:[],
        //测试开始日期
        TTime:"",
        //测试人
        TMan:"",
        //测试数组
        TArray:[],

        //测试完成时间
        ETime:"",

        //通过状态获取订单信息
        getLogByType: function(){
            list.DArray = [];
            list.FArray = [];
            list.TArray = [];
            for(var i= 0,length=list.repairLog.length; i<length;i++){
                var Type = list.repairLog[i].Type;
                switch (Type){
                    //受理
                    case "2":
                        list.GTime = list.repairLog[i].Time;
                        list.GMan = list.repairLog[i].UID[1];
                        break;
                    //开始诊断时间
                    case "3":
                        list.DTime = list.repairLog[i].Time;
                        list.DMan = list.repairLog[i].UID[1];
                        if(list.repairLog[i].Content!=""){
                            list.DArray.push(list.repairLog[i]);
                        }
                        break;
                    //诊断结果
                    case "4":
                        list.FTime = list.repairLog[i].Time; // 维修开始时间
                        if(list.repairLog[i].Content!=""){
                            list.FArray.push(list.repairLog[i]);
                        }
                        break;
                    //维修开始:
                    case "5":
                        list.TTime = list.repairLog[i].Time;// 测试开始时间
                        if(list.repairLog[i].Content!="") {
                            list.TArray.push(list.repairLog[i]);
                        }
                        break;

                    case "6":
                        list.ETime = list.FTime = list.repairLog[i].Time;// 测试结束时间
                        break;



                }
            }
        },

        /*******************************************************************************************/

        /********************************添加处理过程修改审核状态**************************************/
        Content:"",
        /**
         *
         * @param OrderID
         * @param Status
         * @param Flag 0表示返回上一个状态 1表示进入下一个状态 2表示保持当前状态
         */
        addLog: function(OrderID,Status,Flag,fn){

            if(Status<1){
                //状态只有1-8，小于1则为越界
                Status = 1;

            }
            $.call({
                type: 'post',
                i: "7AddLog",
                data: {
                    Type: Status,//list.active
                    Content:list.Content,
                    OrderID:OrderID
                },
                success: function(data){
                    if (data.c == 200) {


                        list.order = data.d;
                        list.Accessories = data.d.Accessories;
                        list.deleteFlag++;
                        if(list.deleteFlag==1){
                            list.orderList.splice(list.index,1);
                        }

                        if(Flag == 1){
                            list.modelActive++;
                        }
                        else if(Flag == 0 ){
                            list.modelActive--;
                        }
                        else if(Flag == 2){
                        }
                        //如果active小于等于1
                        if(list.modelActive<=1){
                            list.modelActive = 1;
                        }
                        if(list.modelActive>=7){
                            list.modelActive = 7;
                        }

                        //如果传入状态为8 将list.modelActive=1重新受理
                       if(Status==1){
                           list.modelActive = 1;
                       }
                        //获取Oder
                        list.getOrder(OrderID,list.index);
                        //清空输入框
                        list.Content = "";
                        tip.on('提交成功', 1, 4000);

                        if(Status==8){
                            $('#myModal').modal('hide')
                            window.location.href='./#!/list/8';
                            window.location.reload()

                        }
                    }
                    else {
                        tip.on('提交失败！',0,6000);
                    }
                }

            });
        },

        //返回上一步
        //returnLast: function (OrderID) {
        //    list.active --;
        //    list.addLog(OrderID);
        //    list.active++;
        //},
        /*******************************************************************************************/


        // 查找表单 和 查找经验
        keyword1: '',  // 查找表单-输入的关键词
        keyword2: '',  // 查找经验-输入的关键词
        experienceList: [],  // 查找经验结果列表

        // 查找表单
        flogS: 1,  // 为1是查找表单，为2是查找经验
        formSearch: function() {
            list.flogS = 1;
            list.search(list.keyword1);
        },

        // 查找经验
        experienceSearch: function() {
            list.flogS = 2;
            list.search(list.keyword2);
        },

        search: function(keyword) {
            $.call({
                type: 'post',
                i: "7Search",
                data: {
                    keyword: keyword
                },
                success: function(data){
                    if (data.c == 200) {

                        // 判断查找结果是否为空
                        if (data.d.length === 0) {
                            tip.on('没有符合条件的表单', 1, 4000);
                        } else {
                            if (list.flogS === 1) {
                                list.orderList = [];
                                list.orderList = data.d;
                            }
                            else {
                                list.experienceList = [];
                                list.experienceList = data.d;
                            }

                        }
                    }
                    else {
                        tip.on('提交失败！',0,6000);
                    }
                }

            });
        }

    });
});